/*
 * Copyright (c) 2010 Mathias Garbe <code@mathias-garbe.de>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "client.hpp"

ThreadingVector<Packet> *LauncherHooks::incomingPacketInstance(ThreadingVector<Packet> *itc, bool reset)
{
    static ThreadingVector<Packet> *i = 0;

    if(itc != 0)
    {
        i = itc;   
    }

    if(reset)
    {
        i = 0;
    }

    return i;
}

ThreadingVector<Packet> *LauncherHooks::outgoingPacketInstance(ThreadingVector<Packet> *itc, bool reset)
{
    static ThreadingVector<Packet> *i = 0;

    if(itc != 0)
    {
        i = itc;   
    }

    if(reset)
    {
        i = 0;
    }

    return i;
}

#ifdef _DEBUG
// enables a console for debug output
void enableConsole()
{
    AllocConsole();

    CONSOLE_SCREEN_BUFFER_INFO conInfo;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &conInfo);
    conInfo.dwSize.Y = 1000;
    SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), conInfo.dwSize);

    // redirect stdin
    long stdHnd = (long)GetStdHandle(STD_INPUT_HANDLE);
    long conHnd = _open_osfhandle(stdHnd, _O_TEXT);

    FILE *fp = _fdopen(conHnd, "r");
    *stdin = *fp;
    setvbuf(stdin, NULL, _IONBF, 0);

    // redirect stdout
    stdHnd = (long)GetStdHandle(STD_OUTPUT_HANDLE);
    conHnd = _open_osfhandle(stdHnd, _O_TEXT);

    fp = _fdopen(conHnd, "w");
    *stdout = *fp;
    setvbuf(stdout, NULL, _IONBF, 0);

    // redirect stderr
    stdHnd = (long)GetStdHandle(STD_ERROR_HANDLE);
    conHnd = _open_osfhandle(stdHnd, _O_TEXT);

    fp = _fdopen(conHnd, "w");
    *stderr = *fp;
    setvbuf(stderr, NULL, _IONBF, 0);

    std::ios::sync_with_stdio();
}
#endif

HANDLE instanceMutex;

int main(int argc, char *argv[])
{
    Qt::QAppInit();

    #ifdef _DEBUG
        enableConsole();
        std::cout << "CnCnet Client Build Version " << _BUILD_VERSION << std::endl << std::endl;
    #endif

    QApplication *qapp = new QApplication(argc, argv);
    qapp->addLibraryPath("plugins/"); 
    qapp->addLibraryPath("/plugins/"); 
    qapp->addLibraryPath("plugins"); 
    qapp->addLibraryPath(qapp->applicationDirPath().append("/plugins/")); 
    Q_INIT_RESOURCE(cncnet);
    Qt::QAppInstance(qapp);

    QMessageBox msgBox;
    msgBox.setWindowTitle("CnCNet Client");
    msgBox.setText("The CnCNet Client has been discontinued, please use the DLLs instead.\n\nPress OK to open your browser to download the DLLs.");
    msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Ok);
    int ret = msgBox.exec();

    if(ret == QMessageBox::Ok)
    {
        QDesktopServices::openUrl(QUrl("http://cncnet.cnc-comm.com/?page=downloads"));
    }

    return 0;


    // Before we do anything, check if this is the only instance
    instanceMutex = CreateMutex(NULL, FALSE, TEXT("CnCNetV2 Instance"));
    if(GetLastError() == ERROR_ALREADY_EXISTS)
    {
        // Welp, second instance.  
        QMessageBox msgBox;
        msgBox.setWindowTitle("CnCNet already started.");
        msgBox.setText("You already have a instance of the client running, please just use that instead opening a new one.");
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.exec();

        // Maybe try to send SW_NORMAL to our only instance to bring it in front of other windows?
        // Look into named pipes for that.

        return 0;
    }

    QSettings settings;

    if(_SETTINGS_VERSION > settings.value("General/Version", "").toInt())
    {
        settings.setValue("General/FirstStart", true);
        settings.sync();
    }

    Update *update = new Update();
    update->startupCheck(settings.value("General/UpdateOnStartup", true).toBool());

    MainWindow win;
    win.show();

    CloseHandle(instanceMutex);

    return 0;
}
